Themes və Publication-Ready Graphics
# Recreate dataset from Part 1
set.seed(2024)
n <- 200
academic_data <- data.frame(
student_id = 1:n,
age = sample(18:25, n, replace = TRUE),
gender = sample(c("Male", "Female"), n, replace = TRUE),
faculty = sample(c("Engineering", "Medicine", "Economics", "IT"), n, replace = TRUE),
math_score = round(rnorm(n, 75, 15), 1),
physics_score = round(rnorm(n, 72, 16), 1),
gpa = round(runif(n, 2.0, 4.0), 2),
study_hours_week = round(pmax(5, rnorm(n, 30, 10)), 1),
life_satisfaction = round(runif(n, 1, 10), 1),
stress_level = round(runif(n, 1, 10), 1),
family_income = round(pmax(1000, rnorm(n, 3000, 1000))),
stringsAsFactors = FALSE
)
academic_data$total_score <- academic_data$math_score + academic_data$physics_score
academic_data$performance_level <- cut(academic_data$gpa,
breaks = c(0, 2.5, 3.0, 3.5, 4.0),
labels = c("Below Average", "Average", "Good", "Excellent"))
cat("Dataset Part 2 üçün hazır:", nrow(academic_data), "tələbə\n")#> Dataset Part 2 üçün hazır: 200 tələbə
# Create sample data for theme demonstration
sample_data <- academic_data[sample(nrow(academic_data), 40), ]
par(mfrow = c(2, 2))
# 1. Minimal style
plot(sample_data$math_score, sample_data$gpa,
main = "theme_minimal() Style",
xlab = "Math Score", ylab = "GPA",
pch = 16, col = "steelblue",
panel.first = grid(col = "lightgray", lty = "dotted"))
box(col = "gray50")
# 2. Classic style
plot(sample_data$math_score, sample_data$gpa,
main = "theme_classic() Style",
xlab = "Math Score", ylab = "GPA",
pch = 16, col = "steelblue")
# 3. Dark theme
plot(sample_data$math_score, sample_data$gpa,
main = "theme_dark() Style",
xlab = "Math Score", ylab = "GPA",
pch = 16, col = "lightblue",
bg = "gray20", fg = "white", col.main = "white",
col.lab = "white", col.axis = "white")
# 4. Publication ready
plot(sample_data$math_score, sample_data$gpa,
main = "Publication Style",
xlab = "Math Score", ylab = "GPA",
pch = 16, col = "black", cex = 0.8,
panel.first = {
grid(col = "gray90")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "gray98", border = "black")
})#> ggplot2 themes:
#> p <- ggplot(data, aes(x = math_score, y = gpa)) + geom_point()
#> p + theme_minimal() # Clean design
#> p + theme_classic() # Classic look
#> p + theme_dark() # Dark background
#> p + theme_bw() # Black and white
# Academic theme
create_academic_plot <- function(x, y, title, xlab, ylab) {
plot(x, y, main = title, xlab = xlab, ylab = ylab,
pch = 16, col = rgb(0.2, 0.4, 0.6, 0.7), cex = 1.1,
panel.first = {
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = rgb(0.95, 0.97, 1.0), border = NA)
grid(col = rgb(1, 1, 1, 0.8), lwd = 1)
},
col.main = "navy", font.main = 2, cex.main = 1.3,
col.lab = "darkblue", font.lab = 2)
box(col = "navy", lwd = 2)
}
create_academic_plot(academic_data$math_score, academic_data$gpa,
"Academic Performance Analysis",
"Mathematics Score", "GPA")#> ggplot2 academic theme:
#> theme_academic <- function() {
#> theme_minimal() +
#> theme(
#> panel.background = element_rect(fill = '#F5F7FF'),
#> plot.background = element_rect(color = 'navy', size = 2),
#> panel.grid = element_line(color = 'white'),
#> plot.title = element_text(color = 'navy', face = 'bold'),
#> axis.title = element_text(color = 'darkblue', face = 'bold')
#> )
#> }
# Corporate theme
create_corporate_plot <- function(x, y, title, xlab, ylab) {
plot(x, y, main = title, xlab = xlab, ylab = ylab,
pch = 15, col = rgb(0.1, 0.1, 0.1, 0.8), cex = 1.0,
panel.first = {
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "white", border = NA)
grid(col = "gray95", lwd = 0.5)
},
col.main = "#2C3E50", cex.main = 1.2,
col.lab = "#34495E", col.axis = "#7F8C8D")
box(col = "#BDC3C7")
}
create_corporate_plot(academic_data$family_income, academic_data$gpa,
"Income Impact on Performance",
"Family Income", "GPA")#>
#> Corporate theme:
#> theme_corporate <- function() {
#> theme_minimal() +
#> theme(
#> panel.background = element_rect(fill = 'white'),
#> panel.grid = element_line(color = 'gray95'),
#> plot.title = element_text(color = '#2C3E50'),
#> axis.title = element_text(color = '#34495E'),
#> legend.position = 'bottom'
#> )
#> }
# Text elements
plot(academic_data$age, academic_data$life_satisfaction,
main = "", xlab = "", ylab = "",
pch = 16, col = rgb(0.3, 0.7, 0.9, 0.7), cex = 1.2)
title(main = "Student Wellbeing Analysis",
col.main = "#2C3E50", cex.main = 1.4, font.main = 2)
title(sub = "Age vs Life Satisfaction",
col.sub = "gray50", cex.sub = 1.0, font.sub = 3)
title(xlab = "Student Age", col.lab = "#34495E", cex.lab = 1.1, font.lab = 2)
title(ylab = "Life Satisfaction Score", col.lab = "#34495E", cex.lab = 1.1, font.lab = 2)#> ggplot2 text elements:
#> ggplot(data, aes(x = age, y = life_satisfaction)) +
#> geom_point() +
#> labs(
#> title = 'Student Wellbeing Analysis',
#> subtitle = 'Age vs Life Satisfaction',
#> x = 'Student Age',
#> y = 'Life Satisfaction Score'
#> ) +
#> theme(
#> plot.title = element_text(size = 16, face = 'bold'),
#> plot.subtitle = element_text(color = 'gray50')
#> )
# Axis customization
plot(academic_data$total_score, academic_data$gpa,
main = "Customized Axes",
xlab = "Total Score", ylab = "GPA",
pch = 21, bg = "lightcoral", col = "darkred",
axes = FALSE)
axis(1, col = "darkred", col.axis = "darkred", cex.axis = 0.9)
axis(2, col = "darkred", col.axis = "darkred", cex.axis = 0.9)
box(col = "darkred")#>
#> Axis customization:
#> theme(
#> axis.text = element_text(color = 'darkred', face = 'bold'),
#> axis.line = element_line(color = 'darkred'),
#> panel.grid = element_line(color = 'gray90', linetype = 'dotted')
#> )
# High-quality academic figure
par(mar = c(5, 5, 4, 2))
plot(academic_data$study_hours_week, academic_data$gpa,
main = "",
xlab = "", ylab = "",
pch = 21, bg = "gray70", col = "black", cex = 0.8,
panel.first = {
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "white", border = "black")
grid(col = "gray90", lty = 1, lwd = 0.5)
})
# Add regression line
lm_pub <- lm(gpa ~ study_hours_week, data = academic_data)
abline(lm_pub, col = "black", lwd = 2)
# Professional labels
title(main = "Study Hours and Academic Performance",
cex.main = 1.1, font.main = 1)
title(xlab = "Study Hours per Week", cex.lab = 1.0)
title(ylab = "Grade Point Average", cex.lab = 1.0)
# Statistics
r_squared <- summary(lm_pub)$r.squared
correlation <- cor(academic_data$study_hours_week, academic_data$gpa)
n_obs <- nrow(academic_data)
text(45, 3.8, sprintf("R² = %.3f", r_squared), cex = 0.9)
text(45, 3.7, sprintf("r = %.3f", correlation), cex = 0.9)
text(45, 3.6, sprintf("n = %d", n_obs), cex = 0.9)#> Publication style:
#> ggplot(data, aes(x = study_hours_week, y = gpa)) +
#> geom_point(shape = 21, fill = 'gray70', color = 'black') +
#> geom_smooth(method = 'lm', color = 'black', se = FALSE) +
#> theme_classic() +
#> theme(
#> plot.title = element_text(size = 11, hjust = 0.5),
#> axis.title = element_text(size = 10),
#> panel.background = element_rect(fill = 'white', color = 'black')
#> )
# Multi-panel figure
par(mfrow = c(2, 2), mar = c(4, 4, 3, 2))
# Panel A: Distribution
hist(academic_data$gpa, breaks = 15,
main = "A) GPA Distribution",
xlab = "GPA", ylab = "Frequency",
col = "gray80", border = "black")
# Panel B: Scatter
plot(academic_data$math_score, academic_data$physics_score,
main = "B) Math vs Physics",
xlab = "Math Score", ylab = "Physics Score",
pch = 16, col = "gray50")
abline(lm(physics_score ~ math_score, data = academic_data), col = "black", lwd = 2)
# Panel C: Box plot
boxplot(gpa ~ performance_level, data = academic_data,
main = "C) GPA by Performance",
xlab = "Performance Level", ylab = "GPA",
col = "gray80")
# Panel D: Bar plot
performance_counts <- table(academic_data$performance_level)
barplot(performance_counts,
main = "D) Performance Distribution",
xlab = "Performance Level", ylab = "Count",
col = "gray80")#>
#> Multi-panel figure:
#> library(gridExtra)
#> p1 <- ggplot() + geom_histogram()
#> p2 <- ggplot() + geom_point()
#> p3 <- ggplot() + geom_boxplot()
#> p4 <- ggplot() + geom_bar()
#> grid.arrange(p1, p2, p3, p4, ncol = 2)
# Interactive simulation
plot(academic_data$total_score, academic_data$life_satisfaction,
main = "Interactive Analysis Simulation",
xlab = "Total Score", ylab = "Life Satisfaction",
pch = 21, bg = rgb(0.3, 0.6, 0.9, 0.7), col = "darkblue", cex = 1.2)
# Simulate tooltip
highlight_student <- academic_data[50, ]
points(highlight_student$total_score, highlight_student$life_satisfaction,
pch = 21, bg = "yellow", col = "red", cex = 2)
tooltip_text <- paste(
"ID:", highlight_student$student_id,
"\nFaculty:", highlight_student$faculty,
"\nGPA:", highlight_student$gpa
)
text(highlight_student$total_score + 15, highlight_student$life_satisfaction,
tooltip_text, pos = 4, cex = 0.8,
bg = "lightyellow", box.col = "orange")#> Interactive plots:
#> library(plotly)
#> p <- ggplot(data, aes(x = total_score, y = life_satisfaction,
#> text = paste('Student:', student_id))) +
#> geom_point() +
#> labs(title = 'Interactive Analysis')
#> ggplotly(p, tooltip = 'text')
# Dashboard simulation
par(mfrow = c(2, 3), mar = c(4, 4, 3, 2))
# 1. GPA distribution
hist(academic_data$gpa, breaks = 10, main = "GPA Distribution",
col = rgb(0.3, 0.7, 0.9, 0.7), border = "darkblue")
# 2. Faculty performance
faculty_means <- aggregate(gpa ~ faculty, data = academic_data, mean)
barplot(faculty_means$gpa, names.arg = faculty_means$faculty,
main = "Average GPA by Faculty", las = 2,
col = rainbow(nrow(faculty_means), alpha = 0.7))
# 3. Study hours vs GPA
plot(academic_data$study_hours_week, academic_data$gpa,
main = "Study vs Performance", pch = 16,
col = rgb(0.6, 0.4, 0.8, 0.6))
# 4. Stress vs satisfaction
plot(academic_data$stress_level, academic_data$life_satisfaction,
main = "Stress vs Satisfaction", pch = 16,
col = rgb(0.8, 0.4, 0.4, 0.6))
# 5. Performance pie
pie(table(academic_data$performance_level),
main = "Performance Distribution",
col = c("red", "orange", "lightgreen", "darkgreen"))
# 6. Subject scores
subjects <- c("Math" = mean(academic_data$math_score),
"Physics" = mean(academic_data$physics_score))
barplot(subjects, main = "Average Scores",
col = c("#E74C3C", "#3498DB"))#>
#> Dashboard creation:
#> library(shiny)
#> ui <- fluidPage(
#> selectInput('faculty', 'Faculty:', choices = unique(data$faculty)),
#> plotOutput('plot')
#> )
#> server <- function(input, output) {
#> output$plot <- renderPlot({
#> filtered <- data[data$faculty == input$faculty, ]
#> ggplot(filtered, aes(x = math_score, y = gpa)) + geom_point()
#> })
#> }
#>
#> Export formats:
#> # High-quality PNG
#> png('figure.png', width = 8, height = 6, units = 'in', res = 300)
#> your_plot_code()
#> dev.off()
#> # PDF for publications
#> pdf('figure.pdf', width = 8, height = 6)
#> your_plot_code()
#> dev.off()
#> # ggplot2 exports
#> ggsave('plot.png', plot = p, width = 8, height = 6, dpi = 300)
#> ggsave('plot.pdf', plot = p, width = 8, height = 6)
#> === PRATİK TAPŞırıq ÖRNƏYİ ===
# Final comprehensive plot
plot(academic_data$study_hours_week, academic_data$gpa,
main = "Final Analysis: Study Habits and Academic Success",
xlab = "Weekly Study Hours", ylab = "Grade Point Average",
pch = 21,
bg = ifelse(academic_data$performance_level == "Excellent", "darkgreen",
ifelse(academic_data$performance_level == "Good", "lightgreen",
ifelse(academic_data$performance_level == "Average", "orange", "red"))),
col = "black", cex = 1.2,
panel.first = {
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "gray98", border = "gray80")
grid(col = "white", lwd = 1)
})
# Add trend line
abline(lm(gpa ~ study_hours_week, data = academic_data),
col = "blue", lwd = 3, lty = 2)
# Legend
legend("bottomright",
legend = c("Excellent", "Good", "Average", "Below Average"),
pch = 21, pt.bg = c("darkgreen", "lightgreen", "orange", "red"),
pt.cex = 1.2, bg = "white", box.col = "gray")
# Add summary statistics
r_val <- cor(academic_data$study_hours_week, academic_data$gpa, use = "complete.obs")
text(15, 3.8, paste("Correlation: r =", round(r_val, 3)),
font = 2, cex = 1.1, col = "blue")#> Comprehensive ggplot2 equivalent:
#> ggplot(academic_data, aes(x = study_hours_week, y = gpa)) +
#> geom_point(aes(fill = performance_level), shape = 21, size = 3) +
#> geom_smooth(method = 'lm', color = 'blue', linetype = 'dashed') +
#> scale_fill_manual(values = c('red', 'orange', 'lightgreen', 'darkgreen')) +
#> labs(
#> title = 'Study Habits and Academic Success',
#> x = 'Weekly Study Hours',
#> y = 'Grade Point Average',
#> fill = 'Performance Level'
#> ) +
#> theme_minimal() +
#> theme(
#> plot.title = element_text(size = 14, face = 'bold', hjust = 0.5),
#> panel.background = element_rect(fill = 'gray98', color = 'gray80'),
#> panel.grid = element_line(color = 'white'),
#> legend.position = 'bottom'
#> )
Bu modulda öyrəndiklərimiz:
Başlanğıc səviyyə:
Orta səviyyə:
Qabaqcıl səviyyə:
Professional və publication-ready visualization-lar yaratmağı öyrəndiniz.
Növbəti addım: Real layihələrdə tətbiq və portfolio yaratmaq
Data visualization journey-nizə uğurlar!
faculty_colors <- c(“Engineering” = “#E74C3C”, “Medicine” = “#3498DB”, “Economics” = “#2ECC71”, “IT” = “#9B59B6”)
plot(academic_data\(math_score, academic_data\)gpa, main = “Faculty Performance”, xlab = “Math Score”, ylab = “GPA”, pch = 16, col = faculty_colors[academic_data$faculty], cex = 1.1)
legend(“bottomright”, legend = names(faculty_colors), col = faculty_colors, pch = 16, cex = 0.8, bg = “white”, box.col = “gray50”, title = “Faculty”)
cat(“customization:”) cat(“theme(”) cat(” legend.position = ‘bottom’,“) cat(” legend.title = element_text(face = ‘bold’),“) cat(” legend.background = element_rect(fill = ‘white’, color = ‘gray50’)“) cat(”)“)